
/**
 * 格式化日期时间
 * @param date 时间
 * @param pattern 格式 默认yyyy-MM-dd HH:mm:ss
 * @returns {*}
 */
 export function formatDateTime(date, pattern = "yyyy-MM-dd HH:mm:ss") {
    if (!date) return date;
    let SIGN_REGEXP = /([yMdHsm])(\1*)/g;
    let DEFAULT_PATTERN = 'yyyy-MM-dd HH:mm:ss';
    if (/^\d+$/.test(date)) {
      date = new Date(+date);
    } else {
      if (typeof date === 'string') {
        date = new Date(date);
      }
    }
    pattern = pattern || DEFAULT_PATTERN;
  
    let padding = function (number, length, prefix = 0) {
      if (String(number).length >= length) {
        return String(number);
      }
      return padding(prefix.toString() + number, length, prefix);
    }
  
    return pattern.replace(SIGN_REGEXP, function ($0) {
      switch ($0.charAt(0)) {
        case 'y':
          return padding(date.getFullYear(), $0.length);
        case 'M':
          return padding(date.getMonth() + 1, $0.length);
        case 'd':
          return padding(date.getDate(), $0.length);
        case 'w':
          return date.getDay() + 1;
        case 'H':
          return padding(date.getHours(), $0.length);
        case 'm':
          return padding(date.getMinutes(), $0.length);
        case 's':
          return padding(date.getSeconds(), $0.length);
      }
    });
  }